//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2005-2007
//
// Select object and zoom in to fill window.
//

# include "StdAfx.h"
# include "ShowRgnOnMap.h"
# include "MapView.h"
# include "Scale.h"
# include "Position.h"
# include "Selection.h"

void ShowRgnOnMap (const rgn_t & _rgn) {
	UnselectObjects ();

	rect_t rect;
	{
		CCritSec::CLock lock (g_cs);

		for (size_t cKind = 0; cKind < rkMax; ++ cKind) {
			layer_t & layer = g_map.layers [cKind];
			rgns_t & rgns = layer.rgns;
			const size_t cRgns = layer.cRgns;

			for (rgns_t::iterator iRgn = rgns.begin (); iRgn != rgns.end (); ++ iRgn) {
				const rgn_t & rgn = * iRgn;
				if (rgn.IsDeleted () || & rgn != & _rgn)
					continue;

				rect.Extend (rgn.rectBound);
			
				find_info_t fi;
				fi.pLayer           = & layer;
				fi.iRgn             = iRgn;
				fi.cElementOrOrigin = -1;
				g_Selection.push_back (fi);
			}
		}
	}

	if (rect.Width () < 2e-2f) {
		rect.x0 -= 1e-2f;
		rect.x1 += 1e-2f;
	}
	if (rect.Height () < 2e-2f) {
		rect.y0 -= 1e-2f;
		rect.y1 += 1e-2f;
	}

	SetScale (GetRectScale (rect), false);
	SetToCenter (point_t (rect.MeanX (), rect.MeanY ()));

	Refresh ();
}
